package org.example.service.managebackend.module.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.service.managebackend.common.domain.dto.ResponseDTO;
import org.example.service.managebackend.module.user.constant.UserResponseConstant;
import org.example.service.managebackend.module.user.domain.dto.LoginFormDTO;
import org.example.service.managebackend.module.user.domain.vo.LoginDetailVO;
import org.example.service.managebackend.module.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Api(tags = "登录")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/user/login/login")
    @ApiOperation("用户登录")
    public ResponseDTO<LoginDetailVO> login(@Valid @RequestBody LoginFormDTO loginForm) {

        String token = loginService.login(loginForm);
        if (null == token) {
            return ResponseDTO.wrap(UserResponseConstant.LOGIN_FAILED);
        }
        LoginDetailVO loginDetail = new LoginDetailVO(token);
        return ResponseDTO.succData(loginDetail);
    }
}
